#include<bits/stdc++.h>
using namespace std;

string id[10010][5];

void change(string &s)
{
	int len = s.size();
	for(int i = 0;i < len;i++)
	{
		if('a' <= s[i] && s[i] <= 'z')
			s[i] -= 'a'-'A';
		else if('A' <= s[i] && s[i] <= 'Z')
			s[i] += 'a'-'A';
	}
}

int main()
{
	string email;
	int n;
	cin>>email>>n;
	int flag = 0;
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= 3;j++)
			cin>>id[i][j];
		if(id[i][3] == email)
		{
			change(id[i][2]);
			flag = 1;
			cout<<id[i][1]<<" "<<id[i][2]<<endl;
		}
	}
	if(!flag)
		cout<<"empty";
    return 0;
}

